﻿using PrjAssessModels;
using PrjAssessModels.BaseModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PrjAssess.Models.Service.AssessProject
{
    public class ProjectDataHelper
    {
        /// <summary>
        /// 仅仅是储存基本信息,不储存材料和工作.
        /// </summary>
        /// <param name="prj"></param>
        /// <returns></returns>
        public static bool SavePrjBasicInfoOnly(Project prj)
        {
            using (PrjAssessDb db = new PrjAssessDb())
            {
                Project existPrj = db.Projects.FirstOrDefault(p => p.PrjId == prj.PrjId);
                DataConvert<Project, Project>.ParsePropterty(prj, existPrj);
                ProjectExtent existExtentPrj = db.ProjectExtents.FirstOrDefault(p => p.PrjId == prj.PrjId);
                DataConvert<ProjectExtent, ProjectExtent>.ParsePropterty(prj.PrjExtent, existExtentPrj);
                db.SaveChanges();
                return true;
            }
        }
        public static Project GetPrjBasicInfoOnly(string prjId)
        {
            if (IsPrjExist(prjId))
            {
                PrjAssessDb db = new PrjAssessDb();
                Project BPI = db.Projects.FirstOrDefault(p => p.PrjId == prjId);
                BPI.PrjExtent = db.ProjectExtents.FirstOrDefault(p => p.PrjId == prjId);
                return BPI;
            }
            else
                return null;
        }

        public static Project GetByPrjId(string prjId)
        {
            if (IsPrjExist(prjId))
            {
                PrjAssessDb db = new PrjAssessDb();
                Project BPI = db.Projects.FirstOrDefault(p => p.PrjId == prjId);
                //DataConvert<Project, Project>.AutoCopy();
                BPI.PrjExtent = db.ProjectExtents.FirstOrDefault(p => p.PrjId == prjId);
                BPI.Works = db.WorkItems.Where(p => p.PrjId == prjId).ToList();
                BPI.MaterialBurys = db.MaterialBuryItems.Where(p => p.PrjId == prjId).ToList();
                return BPI;
            }
            return null;
        }

        public static bool IsPrjExist(string prjId)
        {
            PrjAssessDb db = new PrjAssessDb();
            Project prj = db.Projects.FirstOrDefault(p => p.PrjId == prjId);
            if (prj != null) return true;
            else return false;
        }
        /// <summary>
        /// 储存测算信息,如果存在将覆盖
        /// </summary>
        /// <param name="prj"></param>
        /// <returns></returns>
        public static bool InsertProject(Project prj)
        {
            DeleteByPrjId(prj.PrjId);
            PrjAssessDb db = new PrjAssessDb();
            db.ProjectExtents.Add(prj.PrjExtent);
            db.Projects.Add(prj);

            foreach (var Content in prj.MaterialBurys)
            {
                db.MaterialBuryItems.Add(Content);
            }
            foreach (var Content in prj.Works)
            {
                db.WorkItems.Add(Content);
            }
            db.SaveChanges();
            return true;
        }
        /// <summary>
        /// 删除工程,按照ID
        /// </summary>
        /// <param name="prjId"></param>
        public static void DeleteByPrjId(string prjId)
        {
            PrjAssessDb db = new PrjAssessDb();
            var project = db.Projects.FirstOrDefault(p => p.PrjId == prjId);
            var project_extent = db.ProjectExtents.FirstOrDefault(p => p.PrjId == prjId);
            //
            var content_item_list = db.MaterialBuryItems.Where(item => item.PrjId == prjId).ToList();
            var work_item_list = db.WorkItems.Where(item => item.PrjId == prjId).ToList();

            if (project_extent != null)
            {
               
                //db.Entry(project_extent).State = System.Data.EntityState.Deleted;
                db.ProjectExtents.Remove(project_extent);
                //db.SaveChanges();
                
            }
            if (project != null)
            {
                //db.Entry(project).State = System.Data.EntityState.Deleted;
                db.Projects.Remove(project);
                //db.SaveChanges();
               
            }
           
            if (content_item_list.Count > 0)
            {
                content_item_list.ForEach(
                        item => {
                            //db.Entry(item).State = System.Data.EntityState.Deleted;
                            db.MaterialBuryItems.Remove(item);
                            //db.SaveChanges();
                        }
                    );
                
            }
            if (work_item_list.Count > 0)
            {
                work_item_list.ForEach(item => {
                    //db.Entry(item).State = System.Data.EntityState.Deleted;
                    db.WorkItems.Remove(item); });
               
            }
            db.SaveChanges();
        }


        public static List<PrjItem>  GetAllPrjList()
        {
            PrjAssessDb db = new PrjAssessDb();
            var list = from p in db.Projects
                       orderby p.AssessDate descending
                       select new PrjItem()
                       {
                           Name = p.PrjName,
                           Id = p.PrjId,
                           Adress = p.Address,
                           AssessDate = p.AssessDate
                       };
            return list.ToList();
        }
 
    }
    public class PrjItem
    {
        public string Name { get; set; }
        public string Id { get; set; }
        public string Adress { get; set; }
        public DateTime AssessDate { get; set; }
    }
}